<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useWinPage } from "./table.tsx";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { getList as getBranchList } from "@/api/idp/branch";
import { distributionClient } from "@/api/idp/client";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

const tableRef = ref();
const formRef = ref();

const branchList = ref([]);

onMounted(() => {
  getBranchList().then(res => {
    res.forEach(item => {
      branchList.value.push({
        value: item.id,
        label: item.name
      });
    });
    form.branchId = res[0].id;
  });
});

const clickDistributionClient = () => {
  const clientIds = [];
  currCheckList.value.forEach(item => {
    clientIds.push(item.id);
  });
  distributionClient({
    clientIds: clientIds,
    branchId: form.branchId
  }).then(() => {
    onSearch();
  });
};

const {
  currCheckList,
  form,
  loading,
  columns,
  showDetail,
  onSearch,
  resetForm,
  handleEdit,
  dataList,
  pagination,
  handleSelectionChange
} = useWinPage();
</script>

<template>
  <div>
    <div class="main">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="bg-bg_color w-[99/100] pl-8 pt-4"
      >
        <el-form-item label="ip地址：" prop="user">
          <el-input
            v-model="form.ip"
            placeholder="请输ip地址"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="主机名称：" prop="status">
          <el-input
            v-model="form.hostname"
            placeholder="请输入主机名称"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="分支机构：" prop="status">
          <el-select
            v-model="form.branchId"
            placeholder="请选择分支机构"
            style="width: 240px"
          >
            <el-option
              v-for="item in branchList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon(Search)"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar
        title="Win终端列表"
        :tableRef="tableRef?.getTableRef()"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            type="primary"
            @click="clickDistributionClient"
            :icon="useRenderIcon(AddFill)"
          >
            设备分发
          </el-button>
        </template>
        <template v-slot="{ size, checkList }">
          <pure-table
            ref="tableRef"
            border
            align-whole="center"
            row-key="id"
            showOverflowTooltip
            table-layout="auto"
            default-expand-all
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="columns"
            :checkList="checkList"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-table-row-hover-bg-color)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="handleEdit(row)"
                :icon="useRenderIcon(EditPen)"
              >
                概览
              </el-button>
              <el-popconfirm
                @confirm="showDetail(row)"
                title="操作还没写，有建议可以提"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    操作
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped></style>
